﻿/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 */

package com.seyself.proxy 
{
import flash.accessibility.AccessibilityProperties;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Graphics;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Transform;
import flash.media.SoundTransform;
import flash.text.TextSnapshot;
import flash.ui.ContextMenu;
import flash.utils.Dictionary;

public class SpriteProxy extends Sprite
{
    
	override public function get accessibilityProperties():AccessibilityProperties
	{
		return _target.accessibilityProperties;
	}
	
	override public function get alpha():Number
	{
		return _target.alpha;
	}
	
	override public function get blendMode():String
	{
		return _target.blendMode;
	}
	
	override public function get cacheAsBitmap():Boolean
	{
		return _target.cacheAsBitmap;
	}
	
	override public function get filters():Array
	{
		return _target.filters;
	}
	
	override public function get graphics():Graphics
	{
		return _target.graphics;
	}
	
	override public function get height():Number
	{
		return _target.height;
	}
	
	override public function get loaderInfo():LoaderInfo
	{
		return _target.loaderInfo;
	}
	
	override public function get mask():DisplayObject
	{
		return _target.mask;
	}
	
	override public function get mouseX():Number
	{
		return _target.mouseX;
	}
	
	override public function get mouseY():Number
	{
		return _target.mouseY;
	}
	
	override public function get name():String
	{
		return _target.name;
	}
	
	override public function get opaqueBackground():Object
	{
		return _target.opaqueBackground;
	}
	
	override public function get parent():DisplayObjectContainer
	{
		return _target.parent;
	}
	
	override public function get root():DisplayObject
	{
		return _target.root;
	}
	
	override public function get rotation():Number
	{
		return _target.rotation;
	}
	
	override public function get scale9Grid():Rectangle
	{
		return _target.scale9Grid;
	}
	
	override public function get scaleX():Number
	{
		return _target.scaleX;
	}
	
	override public function get scaleY():Number
	{
		return _target.scaleY;
	}
	
	override public function get scrollRect():Rectangle
	{
		return _target.scrollRect;
	}
	
	override public function get stage():Stage
	{
		return _target.stage;
	}
	
	override public function get transform():Transform
	{
		return _target.transform;
	}
	
	override public function get visible():Boolean
	{
		return _target.visible;
	}
	
	override public function get width():Number
	{
		return _target.width;
	}
	
	override public function get x():Number
	{
		return _target.x;
	}
	
	override public function get y():Number	
	{
		return _target.y;
	}
	
	
	override public function set accessibilityProperties(value:AccessibilityProperties):void
	{
		_target.accessibilityProperties = value;
	}
	
	override public function set alpha(value:Number):void
	{
		_target.alpha = value;
	}
	
	override public function set blendMode(value:String):void
	{
		_target.blendMode = value;
	}
	
	override public function set cacheAsBitmap(value:Boolean):void
	{
		_target.cacheAsBitmap = value;
	}
	
	override public function set filters(value:Array):void
	{
		_target.filters = value;
	}
	
	override public function set height(value:Number):void
	{
		_target.height = value;
	}
	
	override public function set mask(value:DisplayObject):void
	{
		_target.mask = value;
	}
	
	override public function set name(value:String):void
	{
		_target.name = value;
	}
	
	override public function set opaqueBackground(value:Object):void
	{
		_target.opaqueBackground = value;
	}
	
	override public function set rotation(value:Number):void
	{
		_target.rotation = value;
	}
	
	override public function set scale9Grid(value:Rectangle):void
	{
		_target.scale9Grid = value;
	}
	
	override public function set scaleX(value:Number):void
	{
		_target.scaleX = value;
	}
	
	override public function set scaleY(value:Number):void
	{
		_target.scaleY = value;
	}
	
	override public function set scrollRect(value:Rectangle):void
	{
		_target.scrollRect = value;
	}
	
	override public function set transform(value:Transform):void
	{
		_target.transform = value;
	}
	
	override public function set visible(value:Boolean):void
	{
		_target.visible = value;
	}
	
	override public function set width(value:Number):void
	{
		_target.width = value;
	}
	
	override public function set x(value:Number):void
	{
		_target.x = value;
	}
	
	override public function set y(value:Number):void
	{
		_target.y = value;
	}
	
	
	
	
	override public function get contextMenu():ContextMenu
	{
		return _target.contextMenu;
	}
	
	override public function get doubleClickEnabled():Boolean
	{
		return _target.doubleClickEnabled;
	}
	
	override public function get focusRect():Object
	{
		return _target.focusRect;
	}
	
	override public function get mouseEnabled():Boolean
	{
		return _target.mouseEnabled;
	}
	
	override public function get tabEnabled():Boolean
	{
		return _target.tabEnabled;
	}
	
	override public function get tabIndex():int
	{
		return _target.tabIndex;
	}
	
	
	override public function set contextMenu(value:ContextMenu):void
    {
		_target.contextMenu = value;
	}
	
	override public function set doubleClickEnabled(value:Boolean):void
    {
		_target.doubleClickEnabled = value;
	}
	
	override public function set focusRect(value:Object):void
    {
		_target.focusRect = value;
	}
	
	override public function set mouseEnabled(value:Boolean):void
    {
		_target.mouseEnabled = value;
	}
	
	override public function set tabEnabled(value:Boolean):void
    {
		_target.tabEnabled = value;
	}
	
	override public function set tabIndex(value:int):void
	{
		_target.tabIndex = value;
	}
	
	
	
	
	override public function get mouseChildren():Boolean
	{
		return _target.mouseChildren;
	}
	
	override public function get numChildren():int
	{
		return _target.numChildren;
	}
	
	override public function get tabChildren():Boolean
	{
		return _target.tabChildren;
	}
	
	override public function get textSnapshot():TextSnapshot
	{
		return _target.textSnapshot;
	}
	
	
	override public function set mouseChildren(value:Boolean):void
	{
		_target.mouseChildren = value;
	}
	
	override public function set tabChildren(value:Boolean):void
	{
		_target.tabChildren = value;
	}
	
	
	
	
	override public function get buttonMode():Boolean
	{
		return _target.buttonMode;
	}
	
	override public function get dropTarget():DisplayObject
	{
		return _target.dropTarget;
	}
	
	override public function get hitArea():Sprite
	{
		return _target.hitArea;
	}
	
	override public function get soundTransform():SoundTransform
	{
		return _target.soundTransform;
	}
	
	override public function get useHandCursor():Boolean
	{
		return _target.useHandCursor;
	}
	
	
	
	override public function set buttonMode(value:Boolean):void
	{
		_target.buttonMode = value;
	}
	
	override public function set hitArea(value:Sprite):void
	{
		_target.hitArea = value;
	}
	
	override public function set soundTransform(value:SoundTransform):void
	{
		_target.soundTransform = value;
	}
	
	override public function set useHandCursor(value:Boolean):void
	{
		_target.tabChildren = value;
	}
	
	
	
	
	
	
	
    private var _target:Sprite;
    public function get proxyTarget():*
	{
		return _target;
	}
	
    public function SpriteProxy(target:Sprite = null) 
    {
		if (instance.constructor == klass) throw new ArgumentError
			("Error #2012: SpriteProxy$ クラスをインスタンス化することはできません。");
		
		_target = target || new Sprite();
		
		_initialize();
    }
	
	private function _initialize():void
	{
		var n:uint = super.numChildren;
		for (var i:uint = 0; i < n; i++ )
		{
			addChildAt( super.removeChildAt(0), i );
		}
		//super.addChild(_target);
	}
	
	
	
	override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void 
	{
		_target.addEventListener(type, listener, useCapture, priority, useWeakReference);
	}
	
	override public function dispatchEvent(event:Event):Boolean 
	{
		return _target.dispatchEvent(event);
	}
	
	override public function hasEventListener(type:String):Boolean 
	{
		return _target.hasEventListener(type);
	}
	
	override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void 
	{
		_target.removeEventListener(type, listener, useCapture);
	}
	
	override public function willTrigger(type:String):Boolean 
	{
		return _target.willTrigger(type);
	}
	
	
	
	override public function getBounds(targetCoordinateSpace:DisplayObject):Rectangle 
	{
		return _target.getBounds(targetCoordinateSpace);
	}
	
	override public function getRect(targetCoordinateSpace:DisplayObject):Rectangle 
	{
		return _target.getRect(targetCoordinateSpace);
	}
	
	override public function globalToLocal(point:Point):Point 
	{
		return _target.globalToLocal(point);
	}
	
	override public function hitTestObject(obj:DisplayObject):Boolean 
	{
		return _target.hitTestObject(obj);
	}
	
	override public function hitTestPoint(x:Number, y:Number, shapeFlag:Boolean = false):Boolean 
	{
		return _target.hitTestPoint(x, y, shapeFlag);
	}
	
	override public function localToGlobal(point:Point):Point 
	{
		return _target.localToGlobal(point);
	}
	
	
	// DisplayObjectContainer
	
	private var proxyContainer:Dictionary = new Dictionary(true);
	
	override public function addChild(child:DisplayObject):DisplayObject 
	{
		if (child is IProxy)
		{
			var target:DisplayObject = IProxy(child).proxyTarget;
			proxyContainer[target] = child;
			_target.addChild(target);
			return child;
		}
		return _target.addChild(child);
	}
	
	override public function addChildAt(child:DisplayObject, index:int):DisplayObject 
	{
		if (child is IProxy)
		{
			var target:DisplayObject = IProxy(child).proxyTarget;
			proxyContainer[target] = child;
			_target.addChildAt(target, index);
			return child;
		}
		return _target.addChildAt(child, index);
	}
	
	override public function areInaccessibleObjectsUnderPoint(point:Point):Boolean 
	{
		return _target.areInaccessibleObjectsUnderPoint(point);
	}
	
	override public function contains(child:DisplayObject):Boolean 
	{
		if (child is IProxy) return _target.contains(IProxy(child).proxyTarget);
		return _target.contains(child);
	}
	
	override public function getChildAt(index:int):DisplayObject 
	{
		var child:DisplayObject = _target.getChildAt(index);
		if(proxyContainer[child]) return proxyContainer[child];
		return child;
	}
	
	override public function getChildByName(name:String):DisplayObject 
	{
		var child:DisplayObject = _target.getChildByName(name);
		if(proxyContainer[child]) return proxyContainer[child];
		return child;
	}
	
	override public function getChildIndex(child:DisplayObject):int 
	{
		if(child is IProxy) return _target.getChildIndex(IProxy(child).proxyTarget);
		return _target.getChildIndex(child);
	}
	
	override public function getObjectsUnderPoint(point:Point):Array 
	{
		return _target.getObjectsUnderPoint(point);
	}
	
	override public function removeChild(child:DisplayObject):DisplayObject 
	{
		if (child is IProxy)
		{
			var target:DisplayObject = IProxy(child).proxyTarget;
			delete proxyContainer[target];
			_target.removeChild(target);
			return child;
		}
		return _target.removeChild(child);
	}
	
	override public function removeChildAt(index:int):DisplayObject 
	{
		var child:DisplayObject = super.getChildAt(index);
		if (proxyContainer[child])
		{
			var target:DisplayObject = proxyContainer[child];
			delete proxyContainer[child];
			_target.removeChildAt(index);
			return target;
		}
		return _target.removeChildAt(index);
	}
	
	override public function setChildIndex(child:DisplayObject, index:int):void 
	{
		if (child is IProxy) 
			_target.setChildIndex(IProxy(child).proxyTarget, index);
		else 
			_target.setChildIndex(child, index);
	}
	
	override public function swapChildren(child1:DisplayObject, child2:DisplayObject):void 
	{
		if(child1 is IProxy) child1 = IProxy(child1).proxyTarget;
		if(child2 is IProxy) child2 = IProxy(child2).proxyTarget;
		_target.swapChildren(child1, child2);
	}
	
	override public function swapChildrenAt(index1:int, index2:int):void 
	{
		_target.swapChildrenAt(index1, index2);
	}
	
	
	// Sprite
	
    override public function startDrag(lockCenter:Boolean = false, bounds:Rectangle = null):void 
	{
		_target.startDrag(lockCenter, bounds);
	}
	
    override public function stopDrag():void
	{
		_target.stopDrag();
	}
	
    
	
}

}